package com.emiage.ddutils;

import android.util.Log;

import java.util.List;

/**
 * Created by LiJZ on 2016/7/20.
 */
public class LogUtils {
	/**
	 * log tag使用Application Name或者自定义一个
	 */
	private static String TAG = "LJZ";// application name
	private final static int MAX_LOG_SIZE = 4000;
	/**
	 * isPrint or not
	 */
	private static boolean isPrint = true;

	private static LogUtils instance = new LogUtils();

	public static void setTag(String tag) {
		TAG = tag;
	}

	/**
	 * 设置log是否打印以及设置tag
	 * @param tag
	 * @param isPrint	是否打印log
	 */
	public static void setParams(String tag,boolean isPrint){
		isPrint = isPrint;
		TAG = tag;
	}

	private static String getFunctionName() {
		StackTraceElement[] sts = Thread.currentThread().getStackTrace();

		if (sts == null) {
			return null;
		}

		for (StackTraceElement st : sts) {
			if (st.isNativeMethod()) {
				continue;
			}

			if (st.getClassName().equals(Thread.class.getName())) {
				continue;
			}

			if (st.getClassName().equals(
					instance.getClass().getName())) {
				continue;
			}

			return "[" + Thread.currentThread().getName() + "("
					+ Thread.currentThread().getId() + "): " + st.getFileName()
					+ ":" + st.getLineNumber() + "]";
		}

		return null;
	}

	private static String createMessage(String msg) {
		String functionName = getFunctionName();
		String message = (functionName == null ? msg
				: (functionName + " " + msg));
		return message;
	}

	/**
	 * log.i
	 */
	public static void i(String msg) {
		if (isPrint) {

			for(int i = 0; i <= msg.length() / MAX_LOG_SIZE; i++) {
				int start = i * MAX_LOG_SIZE;
				int end = (i+1) * MAX_LOG_SIZE;
				end = end > msg.length() ? msg.length() : end;
				String message = createMessage(msg.substring(start, end));
				Log.i(TAG, message);
			}
		}
	}

	public static void i(String tag, String msg) {
		if (isPrint) {
			for(int i = 0; i <= msg.length() / MAX_LOG_SIZE; i++) {
				int start = i * MAX_LOG_SIZE;
				int end = (i+1) * MAX_LOG_SIZE;
				end = end > msg.length() ? msg.length() : end;
				String message = createMessage(msg.substring(start, end));
				Log.i(tag, message);
			}
		}
	}

	/**
	 * log.v
	 */
	public static void v(String msg) {
		if (isPrint) {
			for(int i = 0; i <= msg.length() / MAX_LOG_SIZE; i++) {
				int start = i * MAX_LOG_SIZE;
				int end = (i+1) * MAX_LOG_SIZE;
				end = end > msg.length() ? msg.length() : end;
				String message = createMessage(msg.substring(start, end));
				Log.v(TAG, message);
			}
		}
	}

	public static void v(String tag, String msg) {
		if (isPrint) {
			for(int i = 0; i <= msg.length() / MAX_LOG_SIZE; i++) {
				int start = i * MAX_LOG_SIZE;
				int end = (i+1) * MAX_LOG_SIZE;
				end = end > msg.length() ? msg.length() : end;
				String message = createMessage(msg.substring(start, end));
				Log.v(tag, message);
			}
		}
	}

	/**
	 * log.d
	 */
	public static void d(String msg) {
		if (isPrint) {
			for(int i = 0; i <= msg.length() / MAX_LOG_SIZE; i++) {
				int start = i * MAX_LOG_SIZE;
				int end = (i+1) * MAX_LOG_SIZE;
				end = end > msg.length() ? msg.length() : end;
				String message = createMessage(msg.substring(start, end));
				Log.d(TAG, message);
			}
		}
	}

	public static void d(String tag, String msg) {
		if (isPrint) {
			for(int i = 0; i <= msg.length() / MAX_LOG_SIZE; i++) {
				int start = i * MAX_LOG_SIZE;
				int end = (i+1) * MAX_LOG_SIZE;
				end = end > msg.length() ? msg.length() : end;
				String message = createMessage(msg.substring(start, end));
				Log.d(tag, message);
			}
		}
	}

	/**
	 * log.e
	 */
	public static void e(String msg) {
		if (isPrint) {
			for(int i = 0; i <= msg.length() / MAX_LOG_SIZE; i++) {
				int start = i * MAX_LOG_SIZE;
				int end = (i+1) * MAX_LOG_SIZE;
				end = end > msg.length() ? msg.length() : end;
				String message = createMessage(msg.substring(start, end));
				Log.e(TAG, message);
			}
		}
	}

	public static void e(String tag, String msg) {
		if (isPrint) {
			for(int i = 0; i <= msg.length() / MAX_LOG_SIZE; i++) {
				int start = i * MAX_LOG_SIZE;
				int end = (i+1) * MAX_LOG_SIZE;
				end = end > msg.length() ? msg.length() : end;
				String message = createMessage(msg.substring(start, end));
				Log.e(tag, message);
			}
		}
	}

	/**
	 * log.d
	 */
	public static void w(String msg) {
		if (isPrint) {
			String message = createMessage(msg);
			Log.w(TAG, message);
		}
	}

	public static void w(String tag, String msg) {
		if (isPrint) {
			String message = createMessage(msg);
			Log.w(tag, message);
		}
	}

//	public static String path = Environment.getExternalStorageDirectory().getPath() +"/Emipian/Cache/push.txt";
//	public synchronized static void f(String tag, String msg) {
//		if (isPrint) {
//			v(tag,msg);
//			String message = createMessage(msg);
//			BaseFileUtil.saveInfo2File(path, message);
//		}
//	}

//	public static void f(String msg) {
//		if (isPrint) {
//			String message = createMessage(msg);
//			f(TAG, message);
//		}
//	}

	/**
	 * log.error
	 */
	public static void error(Exception e) {
		if (isPrint) {
			StringBuffer sb = new StringBuffer();
			String name = getFunctionName();
			StackTraceElement[] sts = e.getStackTrace();

			if (name != null) {
				sb.append(name + " - " + e + "\r\n");
			} else {
				sb.append(e + "\r\n");
			}
			if (sts != null && sts.length > 0) {
				for (StackTraceElement st : sts) {
					if (st != null) {
						sb.append("[ " + st.getFileName() + ":"
								+ st.getLineNumber() + " ]\r\n");
					}
				}
			}
			Log.e(TAG, sb.toString());
		}
	}

	public static void error(String msg, Exception e) {
		if (isPrint) {
			StringBuffer sb = new StringBuffer();
			String name = getFunctionName();
			StackTraceElement[] sts = e.getStackTrace();

			if (name != null) {
				sb.append(name + " - " + msg + '\n' + e + "\r\n");
			} else {
				sb.append(msg + '\n' + e + "\r\n");
			}
			if (sts != null && sts.length > 0) {
				for (StackTraceElement st : sts) {
					if (st != null) {
						sb.append("[ " + st.getFileName() + ":"
								+ st.getLineNumber() + " ]\r\n");
					}
				}
			}
			Log.e(TAG, sb.toString());
		}
	}

	public static void error(String tag, String msg, Exception e) {
		if (isPrint) {
			StringBuffer sb = new StringBuffer();
			String name = getFunctionName();
			StackTraceElement[] sts = e.getStackTrace();

			if (name != null) {
				sb.append(name + " - " + msg + '\n' + e + "\r\n");
			} else {
				sb.append(msg + '\n' + e + "\r\n");
			}
			if (sts != null && sts.length > 0) {
				for (StackTraceElement st : sts) {
					if (st != null) {
						sb.append("[ " + st.getFileName() + ":"
								+ st.getLineNumber() + " ]\r\n");
					}
				}
			}
			Log.e(tag, sb.toString());
		}
	}

	/**
	 * 以log形式打印集合
	 * @param list
	 * @param <T>
	 */
	public static <T> void printList(List<T> list) {
		if (list == null || list.size() < 1) {
			return;
		}
		int size = list.size();
		i("---begin---");
		for (int i = 0; i < size; i++) {
			i(i + ":" + list.get(i).toString());
		}
		i("---end---");
	}

	/**
	 * 以log形式打印数组
	 * @param array
	 * @param <T>
	 */
	public static <T> void printArray(T[] array) {
		if (array == null || array.length < 1) {
			return;
		}
		int length = array.length;
		i("---begin---");
		for (int i = 0; i < length; i++) {
			i(i + ":" + array[i].toString());
		}
		i("---end---");
	}

	public static void e(Exception e) {
		if (isPrint) {
			Log.e(TAG, "", e);
		}
	}
}
